<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>用户管理</title>
  <!-- AdminLTE CSS -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
  <!-- 导航栏 -->
  <nav class="main-header navbar navbar-expand navbar-white navbar-light">
    <!-- 左侧导航链接 -->
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
      </li>
    </ul>

    <!-- 右侧导航链接 -->
    <ul class="navbar-nav ml-auto">
      <li class="nav-item">
        <a class="nav-link" href="/admin/logout" role="button">
          <i class="fas fa-sign-out-alt"></i> 退出登录
        </a>
      </li>
    </ul>
  </nav>

  <!-- 侧边栏 -->
  <aside class="main-sidebar sidebar-dark-primary elevation-4">
    <!-- 品牌logo -->
    <a href="/admin" class="brand-link">
      <span class="brand-text font-weight-light">微信推送管理后台</span>
    </a>

    <!-- 侧边栏菜单 -->
    <div class="sidebar">
      <nav class="mt-2">
        <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
          <li class="nav-item">
            <a href="/admin/users" class="nav-link active">
              <i class="nav-icon fas fa-users"></i>
              <p>用户管理</p>
            </a>
          </li>
          <li class="nav-item">
            <a href="/admin/subscriptions" class="nav-link">
              <i class="nav-icon fas fa-calendar-check"></i>
              <p>订阅管理</p>
            </a>
          </li>
          <li class="nav-item">
            <a href="/admin/push-records" class="nav-link">
              <i class="nav-icon fas fa-history"></i>
              <p>推送记录</p>
            </a>
          </li>
        </ul>
      </nav>
    </div>
  </aside>

  <!-- 内容区域 -->
  <div class="content-wrapper">
    <!-- 内容头部 -->
    <section class="content-header">
      <div class="container-fluid">
        <h1>用户管理</h1>
      </div>
    </section>

    <!-- 主要内容 -->
    <section class="content">
      <div class="container-fluid">
        <div class="card">
          <div class="card-header">
            <h3 class="card-title">用户列表</h3>
          </div>
          <div class="card-body">
            <table id="usersTable" class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>ID</th>
                  <th>OpenID</th>
                  <th>昵称</th>
                  <th>关注时间</th>
                  <th>状态</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
                <% users.forEach(user => { %>
                  <tr>
                    <td><%= user.id %></td>
                    <td><%= user.openid %></td>
                    <td><%= user.nickname || '未设置' %></td>
                    <td><%= new Date(user.subscribeTime).toLocaleString() %></td>
                    <td>
                      <span class="badge <%= user.status === 1 ? 'bg-success' : 'bg-danger' %>">
                        <%= user.status === 1 ? '正常' : '禁用' %>
                      </span>
                    </td>
                    <td>
                      <div class="btn-group">
                        <button class="btn btn-sm btn-info" data-user-id="<%= user.id %>">
                          <i class="fas fa-edit"></i> 编辑
                        </button>
                        <% if(user.status === 1) { %>
                          <button class="btn btn-sm btn-danger" data-user-id="<%= user.id %>">
                            <i class="fas fa-ban"></i> 禁用
                          </button>
                        <% } else { %>
                          <button class="btn btn-sm btn-success" data-user-id="<%= user.id %>">
                            <i class="fas fa-check"></i> 启用
                          </button>
                        <% } %>
                      </div>
                    </td>
                  </tr>
                <% }); %>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </section>
  </div>

  <!-- 页脚 -->
  <footer class="main-footer">
    <div class="float-right d-none d-sm-block">
      <b>Version</b> 1.0.0
    </div>
    <strong>Copyright &copy; 2025 <a href="#">微信推送服务</a>.</strong> All rights reserved.
  </footer>
</div>

<!-- jQuery -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6/dist/jquery.min.js"></script>
<!-- AdminLTE -->
<script src="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/js/adminlte.min.js"></script>
<!-- DataTables -->
<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.4/js/dataTables.bootstrap4.min.js"></script>

<script>
$(function () {
  // 初始化DataTable
  $('#usersTable').DataTable({
    "paging": true,
    "lengthChange": false,
    "searching": true,
    "ordering": true,
    "info": true,
    "autoWidth": false,
    "responsive": true,
    "language": {
      "url": "//cdn.datatables.net/plug-ins/1.13.4/i18n/zh.json"
    }
  });

  // 禁用用户
  $('.btn-danger').on('click', function() {
    const userId = $(this).data('user-id');
    if(confirm('确定要禁用此用户吗？')) {
      $.post(`/admin/users/${userId}/disable`)
        .done(() => {
          toastr.success('用户已禁用');
          setTimeout(() => location.reload(), 1000);
        })
        .fail(() => toastr.error('禁用用户失败'));
    }
  });

  // 启用用户
  $('.btn-success').on('click', function() {
    const userId = $(this).data('user-id');
    if(confirm('确定要启用此用户吗？')) {
      $.post(`/admin/users/${userId}/enable`)
        .done(() => {
          toastr.success('用户已启用');
          setTimeout(() => location.reload(), 1000);
        })
        .fail(() => toastr.error('启用用户失败'));
    }
  });

  // 编辑用户
  $('.btn-info').on('click', function() {
    const button = $(this);
    const row = button.closest('tr');
    const userId = button.data('user-id');
    
    if(button.html().includes('编辑')) {
      // 进入编辑模式
      const nickname = row.find('td:eq(2)').text();
      row.find('td:eq(2)').html(`<input type="text" class="form-control form-control-sm" value="${nickname}">`);
      button.html('<i class="fas fa-save"></i> 保存');
      button.removeClass('btn-info').addClass('btn-warning');
    } else {
      // 保存修改
      const nickname = row.find('td:eq(2) input').val();
      $.ajax({
        url: `/admin/users/${userId}`,
        type: 'PUT',
        contentType: 'application/json',
        data: JSON.stringify({ nickname })
      })
      .done(() => {
        toastr.success('用户信息更新成功');
        setTimeout(() => location.reload(), 1000);
      })
      .fail(() => {
        toastr.error('保存失败');
        location.reload();
      });
    }
  });
});
</script>
</body>
</html>